<?php

namespace app\common\validate;

use think\Validate;
class Base extends Validate
{

	protected $message=[];
	/**
	 * 验证已绑定手机的短信验证码
	 * @param  [type] $v    [description]
	 * @param  [type] $rule [description]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	protected function checkSmsCode($v,$rule,$data){
        $phone = session('member.phone');
        $uid = session('member.id');

        $model = model('sms');
		$sms = $model->checkPhoneSmsCode($phone,$v,$uid);
		if(false==$sms){
			$this->message['smscode.checkSmsCode'] = $query->getError();
			return false;
		}else{
			return true;
		}
    }

    /**
     * 验证未绑定手机的短信验证码
     * @param  [type] $v    [description]
     * @param  [type] $rule [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected function checkSms($v,$rule,$data){
        $phone = $data['phone'];
        $uid = session('member.id');

        $model = model('sms');
        $sms = $model->checkPhoneSmsCode($phone,$v,$uid);
        if(false==$sms){
            $this->message['smscode.checkSms'] = $model->getError();
            return false;
        }else{
            return true;
        }
    }

    /**
     * 验证等级id
     * @param  [type] $v    [description]
     * @param  [type] $rule [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected function checkLevelId($v,$rule,$data){
        $allLevels = model('level')::levelData();
        if(!isset($allLEvels[$v])){
        	$this->message['level_id.checkLevelId'] = '等级错误';
        	return false;
        }else{
        	return true;
        }
    }

    /**
     * 验证货币id
     * @param  [type] $v    [description]
     * @param  [type] $rule [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected function checkCoinId($v,$rule,$data){
        $allCoins = model('coin')::coinData();
        if(!isset($allCoins[$v])){
            $this->message['coin_id.checkCoinId'] = '货币错误';
            return false;
        }else{
            return true;
        }
    }

    /**
     * 验证目标用户
     * @param  [type] $v    [description]
     * @param  [type] $rule [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected function checkTargetMember($v,$rule,$data){
        // 当前用户
        $mid = session('member.mid');
        if($v==$mid){
            $this->message['target.checkTargetMember'] = '目标用户不能是自己';
            return false;
        }
        $model = new \app\common\model\Member;
        if(!$model->where(['mid'=>$v])->find()){
            $this->message['target.checkTargetMember'] = '目标用户不存在';
            return false;
        }
        return true;
    }

    /**
     * 验证银行
     * @param  [type] $v    [description]
     * @param  [type] $rule [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected function checkBankId($v,$rule,$data){
        $allBanks = model('bank')::bankStatus(1);
        if(!isset($allBanks[$v])){
            $this->message[$rule.'.checkBankId'] = '银行错误';
            return false;
        }else{
            return true;
        }
    }

    /**
     * 验证登录名
     * @param  [type] $v    [description]
     * @param  [type] $rule [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected function checkLoginName($v,$rule,$data){
        $user = model('member')->where(['mid'=>$v])->whereOr(['phone'=>$v])->find();
        if($user){
            return true;
        }else{
            $this->message[$rule.'.checkLoginName'] = '用户不存在';
            return false;
        }
    }

    /**
     * 验证电话号码格式
     * @param  [type] $v    [description]
     * @param  [type] $rule [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected function checkPhone($v,$rule,$data){
        $ptn = "/^1[3578]\d{9}$/";
        if(! preg_match($ptn, $v)) return false;
        return true;
    }

}